%%-*- mode: erlang -*-
%% emq_auth_mongo config mapping
{mapping, "auth.mongo.server", "emq_auth_mongo.server", [
  {default, {"127.0.0.1", 27017}},
  {datatype, [integer, ip, string]}
]}.

{mapping, "auth.mongo.pool", "emq_auth_mongo.server", [
  {default, 8},
  {datatype, integer}
]}.

{mapping, "auth.mongo.login", "emq_auth_mongo.server", [
  {default, ""},
  {datatype, string}
]}.

{mapping, "auth.mongo.password", "emq_auth_mongo.server", [
  {default, ""},
  {datatype, string}
]}.

{mapping, "auth.mongo.database", "emq_auth_mongo.server", [
  {default, "mqtt"},
  {datatype, string}
]}.

{translation, "emq_auth_mongo.server", fun(Conf) ->
  {MgHost, MgPort} =
  case cuttlefish:conf_get("auth.mongo.server", Conf) of
    {Ip, Port} -> {Ip, Port};
    S          -> case string:tokens(S, ":") of
                    [Domain]       -> {Domain, 27017};
                    [Domain, Port] -> {Domain, list_to_integer(Port)}
                  end
    end,
  Pool = cuttlefish:conf_get("auth.mongo.pool", Conf),
  Login = cuttlefish:conf_get("auth.mongo.login", Conf),
  Passwd = cuttlefish:conf_get("auth.mongo.password", Conf),
  DB = cuttlefish:conf_get("auth.mongo.database", Conf),
  Login0 = case Login =:= [] of
    true -> [];
    false -> [{login, Login}]
  end,
  Passwd0 = case Passwd =:= [] of
    true -> [];
    false -> [{password, Passwd}]
  end,
  [{pool_size, Pool},
   {auto_reconnect, 1},
   {host, MgHost},
   {port, MgPort},
   {database, DB}] ++ Login0 ++ Passwd0
end}.

{mapping, "auth.mongo.auth_query.collection", "emq_auth_mongo.auth_query", [
  {default, "mqtt_user"},
  {datatype, string}
]}.

{mapping, "auth.mongo.auth_query.password_field", "emq_auth_mongo.auth_query", [
  {default, "password"},
  {datatype, string}
]}.

{mapping, "auth.mongo.auth_query.password_hash", "emq_auth_mongo.auth_query", [
  {default, sha256},
  {datatype, string}
]}.

{mapping, "auth.mongo.pbkdf2_macfun", "emq_auth_mongo.pbkdf2_macfun", [
  {datatype, atom}
]}.

{mapping, "auth.mongo.pbkdf2_iterations", "emq_auth_mongo.pbkdf2_iterations", [
  {datatype, integer}
]}.

{mapping, "auth.mongo.pbkdf2_dklen", "emq_auth_mongo.pbkdf2_dklen", [
  {datatype, integer}
]}.

{mapping, "auth.mongo.auth_query.selector", "emq_auth_mongo.auth_query", [
  {default, ""},
  {datatype, string}
]}.

{translation, "emq_auth_mongo.auth_query", fun(Conf) ->
  Collection = cuttlefish:conf_get("auth.mongo.auth_query.collection", Conf),
  PasswordField = cuttlefish:conf_get("auth.mongo.auth_query.password_field", Conf),
  PasswordHash = cuttlefish:conf_get("auth.mongo.auth_query.password_hash", Conf),
  Selector = cuttlefish:conf_get("auth.mongo.auth_query.selector", Conf),
  PasswordFields = [list_to_binary(Field) || Field <- string:tokens(PasswordField, " ")],
  HashValue =
    case string:tokens(PasswordHash, " ") of
          [Hash]           -> list_to_atom(Hash);
          [Prefix, Suffix] -> {list_to_atom(Prefix), list_to_atom(Suffix)};
          _                -> plain
    end,
  [{collection, Collection},
  {password_field, PasswordFields},
  %% Hash Algorithm: plain, md5, sha, sha256, pbkdf2?
  {password_hash, HashValue},
  {selector, Selector}
]
end}.

{mapping, "auth.mongo.super_query", "emq_auth_mongo.super_query", [
  {default, off},
  {datatype, flag}
]}.

{mapping, "auth.mongo.super_query.collection", "emq_auth_mongo.super_query", [
  {default, "mqtt_user"},
  {datatype, string}
]}.

{mapping, "auth.mongo.super_query.super_field", "emq_auth_mongo.super_query", [
  {default, "is_superuser"},
  {datatype, string}
]}.

{mapping, "auth.mongo.super_query.selector", "emq_auth_mongo.super_query", [
  {default, ""},
  {datatype, string}
]}.

{translation, "emq_auth_mongo.super_query", fun(Conf) ->
  case cuttlefish:conf_get("auth.mongo.super_query", Conf) of
    false -> cuttlefish:unset();
    true  -> Collection = cuttlefish:conf_get("auth.mongo.super_query.collection", Conf),
             SuperField = cuttlefish:conf_get("auth.mongo.super_query.super_field", Conf),
             Selector = cuttlefish:conf_get("auth.mongo.super_query.selector", Conf),
             [{collection, Collection}, {super_field, SuperField}, {selector, Selector}]
  end
end}.

{mapping, "auth.mongo.acl_query", "emq_auth_mongo.acl_query", [
  {default, off},
  {datatype, flag}
]}.

{mapping, "auth.mongo.acl_query.collection", "emq_auth_mongo.acl_query", [
  {default, "mqtt_user"},
  {datatype, string}
]}.

{mapping, "auth.mongo.acl_query.selector", "emq_auth_mongo.acl_query", [
  {default, ""},
  {datatype, string}
]}.

{translation, "emq_auth_mongo.acl_query", fun(Conf) ->
  case cuttlefish:conf_get("auth.mongo.acl_query", Conf) of
    false -> cuttlefish:unset();
    true  -> Collection = cuttlefish:conf_get("auth.mongo.acl_query.collection", Conf),
             Selector = cuttlefish:conf_get("auth.mongo.acl_query.selector", Conf),
             [{collection, Collection}, {selector, Selector}]
  end
end}.

{mapping, "auth.mongo.acl_nomatch", "emq_auth_mongo.acl_nomatch", [
  {default, deny},
  {datatype, {enum, [deny, allow]}}
]}.
